<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>爱家-房间信息</title>
    <link href="/static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="/static/css/reset.css" rel="stylesheet">
    <link href="/static/plugins/swiper/css/swiper.min.css" rel="stylesheet">
    <link href="/static/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css" rel="stylesheet">
    <link href="/static/css/ihome/main.css" rel="stylesheet">
    <link href="/static/css/ihome/detail.css" rel="stylesheet">
</head>

<body>
    <div class="container">
        <div class="top-bar">
            <div class="nav-bar">
                <h3 class="page-title">房间信息</h3>
                <a class="nav-btn fl" href="#" onclick="hrefBack();"><span><i
                            class="fa fa-angle-left fa-2x"></i></span></a>
            </div>
        </div>
        <div class="swiper-container">
            <script id="swiper-tmpl" type="text/html">
                <ul class="swiper-wrapper">
                    {{each house_info.image_urls}}
                    <li class="swiper-slide"><img src="{{$value}}"></li>
                    {{/each}}
                </ul>
                <div class="swiper-pagination"></div>
                <div class="house-price">￥<span>{{house_info.price}}</span>/晚</div>
            </script>
        </div>
        <div class="detail-con">
            <script type="text/html" id="house-detail-tmpl">
                <div class="detail-header layout-style">
                    <h2 class="house-title">{{house_info.title}}</h2>
                    <div class="landlord-pic"><img src="{{house_info.user_avatar}}"></div>
                    <h2 class="landlord-name">房东： <span>{{house_info.nickname}}</span></h2>
                </div>
                <div class="house-info layout-style">
                    <h3>房屋地址</h3>
                    <ul class="house-info-list text-center">
                        <li>{{house_info.address}}</li>
                    </ul>
                </div>
                <ul class="house-type layout-style">
                    <li>
                        <span class="icon-house"></span>
                        <div class="icon-text">
                            <h3>出租{{house_info.room_count}}间</h3>
                            <p>房屋面积:{{house_info.acreage}}平米</p>
                            <p>房屋户型:{{house_info.unit}}</p>
                        </div>
                    </li>
                    <li>
                        <span class="icon-user"></span>
                        <div class="icon-text">
                            <h3>宜住{{house_info.capacity}}人</h3>
                        </div>
                    </li>
                    <li>
                        <span class="icon-bed"></span>
                        <div class="icon-text">
                            <h3>卧床配置</h3>
                            <p>{{house_info.beds}}</p>
                        </div>
                    </li>
                </ul>
                <div class="house-info layout-style">
                    <h3>房间详情</h3>
                    <ul class="house-info-list">
                        <li>收取押金<span>{{house_info.deposit}}</span></li>
                        <li>最少入住天数<span>{{house_info.min_days}}</span></li>
                        <li>最多入住天数<span>{{if house_info.max_days==0}}无限制{{else}}{{house_info.max_days}}{{/if}}</span></li>
                    </ul>
                </div>
                <div class="house-facility layout-style">
                    <h3>配套设施</h3>
                    <ul class="house-facility-list clearfix">
                        {{include 'device-tmpl' devices_info}}
                    </ul>
                </div>
                {{if house_info.comments.length > 0 }}
                <div class="house-info layout-style">
                    <h3>评价信息</h3>
                    <ul class="house-comment-list">
                        {{each house_info.comments}}
                        <li>
                            <p>{{$value.nickname}}<span class="fr">{{$value.ctime}}</span></p>
                            <p>{{$value.comment}}</p>
                        </li>
                        {{/each}}
                    </ul>
                </div>
                {{/if}}
            </script>
            <script id="device-tmpl" type="text/html">
                {{each $data.devices}}
                <li><span class="{{if $data.select.indexOf($index+1)>=0}}{{$value.icon}}{{else}}jinzhi-ico{{/if}}"></span>{{$value.name}}</li>
                {{/each}}
            </script>
        </div>
        <a id="book-house" class="book-house" href="/booking.html">即刻预定</a>
        <a id="delete-house" class="book-house" href="javascript:void()">删除房源</a>
        <div class="footer">
            <p><span><i class="fa fa-copyright"></i></span>爱家租房&nbsp;&nbsp;享受家的温馨</p>
        </div>
    </div>

    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script src="/static/plugins/swiper/js/swiper.jquery.min.js"></script>
    <script src="/static/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>
    <script src="/static/plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>
    <script src="/static/js/template.js"></script>
    <script>
        var params = decodeQuery();

        function getCookie(name) {
            var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
            return r ? r[1] : undefined;
        }

        function hrefBack() {
            history.go(-1);
        }

        function decodeQuery() {
            var search = decodeURI(document.location.search);
            return search.replace(/(^\?)/, '').split('&').reduce(function (result, item) {
                values = item.split('=');
                result[values[0]] = values[1];
                return result;
            }, {});
        }

        $(document).ready(function () {
            $.get("/api/v1/devices", function (resp) {
                if (resp.errno == "0") {
                    // 使用js模板
                    var devices = resp.data
                    $.get("/api/v1/houses/" + params.id, function (resp) {
                        if (resp.errno == "0") {
                            $(".swiper-container").html(template('swiper-tmpl', { house_info: resp.data.house }))
                            var mySwiper = new Swiper('.swiper-container', {
                                loop: true,
                                autoplay: 2000,
                                autoplayDisableOnInteraction: false,
                                pagination: '.swiper-pagination',
                                paginationType: 'fraction'
                            })

                            if (resp.data.user_id != resp.data.house.user_id) {
                                $("#book-house").attr("href", "/booking.html?hid=" + resp.data.house.house_id);
                                $("#book-house").show();
                            } else {
                                $("#delete-house").show()
                                $("#delete-house").on("click", function () {
                                    $.ajax({
                                        url: "/api/v1/houses/" + params.id + "/delete",
                                        type: 'delete',
                                        dataType: 'json',
                                        headers:{
                                            'X-CSRFToken': getCookie('csrf_token')
                                        },
                                        success(resp) {
                                            if (resp.errno == "0") {
                                                history.back()
                                            } else {
                                                alert(resp.errmsg)
                                            }
                                        }
                                    })
                                })
                            }

                            $(".detail-con").html(template('house-detail-tmpl', { house_info: resp.data.house, devices_info: { devices: devices, select: resp.data.house.devices } }))

                        } else if (resp.errno == "4001") {
                            window.location.href = "/login.html"
                        } else {
                            alert(resp.errmsg)
                        }
                    }, 'json')
                } else {
                    alert(resp.errmsg);
                }
            }, "json");
        })
    </script>
</body>

</html>